import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hxdj/common/colors/my_colors.dart';
import 'package:hxdj/common/components/my_image.dart';
import 'package:hxdj/common/components/my_button.dart';
import 'package:hxdj/common/components/my_dimens.dart';
import 'package:hxdj/common/components/my_gaps.dart';

/// 自定义AppBar
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  const MyAppBar(
      {super.key,
      this.backgroundColor,
      this.title = '',
      this.actionName = '',
      this.actionImageName = '',
      this.backImgName = "ic_back_black",
      this.backImgColor,
      this.onPressed,
      this.isBack = true,
      this.leftWidget,
      this.titleWidget,
      this.rightWidget});

  final Color? backgroundColor;
  final String title;
  final String backImgName;
  final Color? backImgColor;
  final String actionName;
  final String actionImageName;
  final VoidCallback? onPressed;
  final bool isBack;
  final Widget? leftWidget;
  final Widget? titleWidget;
  final Widget? rightWidget;

  @override
  Widget build(BuildContext context) {
    final Color bgColor = backgroundColor ?? Colors.white;

    final SystemUiOverlayStyle overlayStyle = ThemeData.estimateBrightnessForColor(bgColor) == Brightness.dark
        ? SystemUiOverlayStyle.light
        : SystemUiOverlayStyle.dark;

    final Widget action = rightWidget == null
        ? ((actionName.isNotEmpty || actionImageName.isNotEmpty)
            ? Positioned(
                right: 0.0,
                child: Theme(
                  data: Theme.of(context).copyWith(
                    buttonTheme: const ButtonThemeData(
                      padding: EdgeInsets.symmetric(horizontal: 16.0),
                      minWidth: 60.0,
                    ),
                  ),
                  child: actionName.isNotEmpty
                      ? MyButton(
                          key: const Key('actionName'),
                          fontSize: MyDimens.fontSp14,
                          minWidth: null,
                          text: actionName,
                          textColor: MyColours.text,
                          backgroundColor: Colors.transparent,
                          onPressed: onPressed,
                        )
                      : actionImageName.isNotEmpty
                          ? GestureDetector(
                              onTap: () {
                                onPressed!();
                              },
                              child: Container(
                                padding: const EdgeInsets.fromLTRB(5, 5, 15, 5),
                                child: MyImage(
                                  imageName: actionImageName,
                                  width: 20,
                                ),
                              ),
                            )
                          : Container(),
                ),
              )
            : MyGaps.empty)
        : Positioned(
            right: 15.0,
            child: Theme(
                data: Theme.of(context).copyWith(
                  buttonTheme: const ButtonThemeData(
                    padding: EdgeInsets.symmetric(horizontal: 16.0),
                    minWidth: 60.0,
                  ),
                ),
                child: rightWidget!));

    final Widget back = isBack
        ? IconButton(
            onPressed: () async {
              FocusManager.instance.primaryFocus?.unfocus();
              final isBack = await Navigator.maybePop(context);
              if (!isBack) {
                await SystemNavigator.pop();
              }
            },
            tooltip: 'Back',
            padding: const EdgeInsets.all(12.0),
            icon: MyImage(
              imageName: backImgName,
              width: 10,
              height: 15,
            ))
        : MyGaps.empty;

    final Widget titleWidget = Semantics(
      namesRoute: true,
      header: true,
      child: Container(
        alignment: Alignment.center,
        width: double.infinity,
        margin: const EdgeInsets.symmetric(horizontal: 48.0),
        child: Text(
          title.isEmpty ? "" : title,
          maxLines: 1,
          style: TextStyle(
            fontSize: MyDimens.fontSp18,
            fontWeight: FontWeight.bold,
            color: MyColours.text,
          ),
        ),
      ),
    );

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: overlayStyle,
      child: Material(
        color: bgColor,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              titleWidget,
              back,
              action,
            ],
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(48.0);
}
